package com.android.jay.lib_audio.mediaplayer.db;

import android.database.sqlite.SQLiteDatabase;

import com.android.jay.lib_audio.app.AudioHelper;
import com.android.jay.lib_audio.mediaplayer.model.AudioBean;
import com.android.jay.lib_audio.mediaplayer.model.Favourite;

/**
 * Created by wzp on 2020/1/31 0031.
 * <p>
 * 描述  ：todo
 */
public class GreenDaoHelper {

    private static final String DB_NAME = "music_db";
    // 数据库帮助类用来创建数据库 升级数据库
    private static DaoMaster.DevOpenHelper mHelper;
    //最终创建好的数据库
    private static SQLiteDatabase mDb;
    //用来直接管理数据库
    private static DaoMaster mDaoMaster;
    //管理表
    private static DaoSession mDaoSession;


    //初始化数据库
    public static void initDatabase() {
        mHelper = new DaoMaster.DevOpenHelper(AudioHelper.getContext(), DB_NAME,null);
        mDb = mHelper.getWritableDatabase();
        mDaoMaster = new DaoMaster(mDb);
        mDaoSession = mDaoMaster.newSession();
    }

    //添加收藏
    public static void addFavourite(AudioBean bean) {
        FavouriteDao dao = mDaoSession.getFavouriteDao();
        Favourite favourite = new Favourite();
        favourite.setAudioId(bean.id);
        favourite.setAudioBean(bean);
        dao.insertOrReplace(favourite);
    }

    //移除感兴趣
    public static void removeFavourite(AudioBean bean) {
        FavouriteDao dao = mDaoSession.getFavouriteDao();
        Favourite favourite
                = selectFavourite(bean);
        dao.delete(favourite);
    }

    //查找感兴趣的
    public static Favourite selectFavourite(AudioBean bean) {
        FavouriteDao dao = mDaoSession.getFavouriteDao();
        Favourite favourite
                = dao.queryBuilder().where(FavouriteDao.Properties.AudioId.eq(bean.id)).unique();
        return favourite;
    }


}
